import { defineStore } from 'pinia'

interface Userinfo {
  username: string
  nickname: string
  email: string
  email_verified: string
  sub: string
  picture: string
  'custom:company': string
  'custom:city': string
  'custom:industry': string
  'custom:workPosition': string
  'custom:phone': string
  'custom:IdentityLabel': string
}

interface Authentication {
  expirationTime: string
  expiresIn: string
  refreshToken: string
  token: string
}

export default defineStore('user', () => {
  const userinfo = ref<Userinfo>({
    username: '',
    nickname: '',
    email: '',
    email_verified: '',
    sub: '',
    picture: '',
    'custom:company': '',
    'custom:city': '',
    'custom:industry': '',
    'custom:workPosition': '',
    'custom:phone': '',
    'custom:IdentityLabel': '',
  })

  const authentication = ref<Authentication>({
    token: '',
    expirationTime: '',
    expiresIn: '',
    refreshToken: '',
  })

  function login($userinfo: Userinfo, $authentication: Authentication) {
    userinfo.value = $userinfo
    authentication.value = $authentication
  }

  function updateToken(token: string) {
    authentication.value.token = token
  }
  return {
    userinfo,
    authentication,
    login,
    updateToken,
    username: computed(() => userinfo.value?.username),
    token: computed(() => authentication.value.token),
  }
})
